﻿using System;
using System.Collections.Generic;

namespace TodoMVC.Tools
{
    public static class EnumHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TEnumUnderlyingType"></typeparam>
        /// <param name="enumType"></param>
        /// <param name="isCaption"></param>
        /// <returns></returns>
        public static Dictionary<string, TEnumUnderlyingType> ConvertEnumToDictonary<TEnumUnderlyingType>(Type enumType, bool isCaption = false)
        {
            if (!enumType.IsEnum)
            {
                throw new ArgumentException(nameof(enumType));
            }
            var values = Enum.GetValues(enumType);
            var dict = new Dictionary<string, TEnumUnderlyingType>();
            foreach (var value in values)
            {
                var name = Enum.GetName(enumType, value);
                if (!isCaption)
                {
                    if (name.Length > 1)
                    {
                        name = name.Substring(0, 1).ToLower() + name.Substring(1);
                    }
                    else
                    {
                        name = name.ToLower();
                    }

                }
                dict.Add(name, (TEnumUnderlyingType)value);
            }
            return dict;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="isCaption"></param>
        /// <returns></returns>
        public static Dictionary<string, int> ConvertEnumToDictonary(Type enumType, bool isCaption = false)
        {
            return ConvertEnumToDictonary<int>(enumType, isCaption);
        }
    }
}
